package com.py.credit.service.impl;

import com.py.credit.dao.DataCoverageDao;
import com.py.credit.entity.CoverageData;
import com.py.credit.entity.DataTableEntity;
import com.py.credit.entity.Table;
import com.py.credit.service.DataCoverageService;
import com.py.credit.utils.PercentUtil;
import com.py.framework.core.common.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 功能描述:覆盖率计算类
 *
 * @author YaoS
 * @date 19/4/9 11:18
 */
@Service
public class DataCoverageServiceImpl implements DataCoverageService {

    @Autowired
    private DataCoverageDao dataCoverageDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertData() {
        List<String> list = dataCoverageDao.selectTableName();
        Integer molecule = 0;
        Integer denominator = dataCoverageDao.bodyCorporateCount();
        if (!CollectionUtils.isEmpty(list)) {
            //查询所有表去重后归集数据
            molecule = dataCoverageDao.selectDataCount(list);
        }
        //计算整体覆盖率
        String percent = PercentUtil.getPercent(molecule, denominator);
        CoverageData col = new CoverageData();
        col.setEntityCount(denominator);
        col.setI(molecule);
        col.setPercent(percent);
        //删除原数据
        dataCoverageDao.deleteDataCount();
        //插入数据
        dataCoverageDao.insertDataCount(col);

        try {
            //获取全部的表信息
            List<Table> tableList = dataCoverageDao.selectTable();
            List<DataTableEntity> dataTableEntities = new ArrayList<>();
            if (!CollectionUtils.isEmpty(tableList)) {
                for (Table table : tableList) {
                    String tableName = "t_" + table.getResourceId();
                    //获取单个资源信息总数
                    int count = dataCoverageDao.getAllDataCount(tableName);
                    //获取单个资源已归集去重后总数
                    int disCount = dataCoverageDao.getDisDataCount(tableName);
                    percent = PercentUtil.getPercent(disCount, count);
                    DataTableEntity dataTableEntity = new DataTableEntity();
                    dataTableEntity.setTableName(table.getResourceTitle());
                    dataTableEntity.setPerCent(percent);
                    dataTableEntity.setAlsoEntityCount(molecule);
                    dataTableEntity.setTableId(table.getTableId());
                    dataTableEntity.setTableCount(count);
                    dataTableEntity.setTableEntityCount(disCount);
                    dataTableEntities.add(dataTableEntity);
                }
            }

            dataCoverageDao.deleteColumnTable();
            if (dataTableEntities.size() > 0) {
                dataCoverageDao.insertColumnTable(dataTableEntities);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    @Override
    public List<Map<String, Object>> selectInsertDataCount() {
        return dataCoverageDao.selectInsertDataCount();
    }

    @Override
    public List<Map<String, Object>> selectInsertColumnTable(int tableid) {
        return dataCoverageDao.selectInsertColumnTable(tableid);
    }

    @Override
    public List<Map<String, Object>> selectInsertColumnTableForPage(Page page) {
        return dataCoverageDao.selectInsertColumnTableForPage(page);
    }

    @Override
    public List<Map<String, Object>> selectInsertDeptCountForPage(Page page, int tableId) {
        String tableName = "t_" + dataCoverageDao.getTableById(tableId).getResourceId();
        return dataCoverageDao.selectInsertDeptCountForPage(page, tableName);
    }

}
